﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/9/9 星期一 22:47:40
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Framework.DB;
using Sector7.Medical.EntityType;
using Sector7.Medical.Consts;
using Sector7.Medical.DAO.Utility;

namespace Sector7.Medical.DAO.Standard
{
    /// <summary>
    /// 药品规格数据访问对象
    /// </summary>
    public class DrugSpecDAO : DAOBase<M7_DrugSpec>, IDrugSpecDAO
    {
        #region Public functions

        public List<M7_DrugSpec> ReadAll(long drugId)
        {
            List<M7_DrugSpec> specList = new List<M7_DrugSpec>();

            string sql = @"
                SELECT
                    ds.*,
                    uu.UnitId UU_UnitId,
                    uu.UnitName UU_UnitName,
                    pu.UnitId PU_UnitId,
                    pu.UnitName PU_UnitName,
                    su.UnitId SU_UnitId,
                    su.UnitName SU_UnitName
                FROM
                    M7_DrugSpec ds
                    LEFT JOIN M7_Unit uu ON ds.UsageUnitId = uu.UnitId
                    LEFT JOIN M7_Unit pu ON ds.PackUnitId = pu.UnitId
                    LEFT JOIN M7_Unit su ON ds.StockUnitId = su.UnitId
                WHERE
                    ds.DrugId = @DrugId";

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(drugId, C_ColumnName.DRUG_ID));

                while (reader.Read())
                {
                    var drugSpec = ResolveToDrugSpec(reader);
                    specList.Add(drugSpec);
                }
            }

            return specList;
        }

        #endregion

        #region Private functions

        private M7_DrugSpec ResolveToDrugSpec(SqlDataReader reader)
        {
            var drugSpec = new M7_DrugSpec();
            var usageUnit = new M7_Unit();
            var packUnit = new M7_Unit();
            var stockUnit = new M7_Unit();

            object[] values = new object[reader.FieldCount];
            reader.GetValues(values);
            for (int i = 0; i < reader.FieldCount; ++i)
            {
                string propertyName = reader.GetName(i);
                object propertyValue = values[i];

                FillEntityUtil.ToDrugSpec(drugSpec, propertyName, propertyValue);
                FillEntityUtil.ToUnit(usageUnit, "UU_", propertyName, propertyValue);
                FillEntityUtil.ToUnit(packUnit, "PU_", propertyName, propertyValue);
                FillEntityUtil.ToUnit(stockUnit, "SU_", propertyName, propertyValue);
            }

            drugSpec.UsageUnit = usageUnit;
            drugSpec.PackUnit = packUnit;
            drugSpec.StockUnit = stockUnit;

            return drugSpec;
        }

        #endregion
    }
}
